/*******************************************************************************
 * Copyright (c) 2010 - 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Lars Vogel <lars.Vogel@gmail.com> - Bug 419770
 *******************************************************************************/
package planclient;

import java.util.Date;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import modele.Employes;
import modele.Employe;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.internal.runtime.InternalPlatform;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.EclipseContextFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.e4.ui.model.application.ui.MContext;
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
import org.eclipse.e4.ui.model.application.ui.basic.MBasicFactory;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;


public class listeEmployePart implements  IDoubleClickListener,ISelectionChangedListener{

	private Text txtInput;
	private TableViewer tableViewer;
	Employes listeEmployes;

	@Inject
	private MDirtyable dirty;

	@PostConstruct
	public void createComposite(Composite parent) {
		parent.setLayout(new GridLayout(1, false));

		/*txtInput = new Text(parent, SWT.BORDER);
		txtInput.setMessage("Enter text to mark part as dirty");
		txtInput.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				dirty.setDirty(true);
			}
		});
		*/
		//txtInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		listeEmployes = Employes.Factory();
		
		tableViewer = new TableViewer(parent);
		tableViewer.addDoubleClickListener(this);
		tableViewer.addSelectionChangedListener(this);

		for (int i=0; i< listeEmployes.num ; i ++ ){
			Employe crt = listeEmployes.employes.get(i);
			//String m = crt.getPrenom() + " " + crt.getNom() ;
			tableViewer.add(crt);
		}
		tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
		
	
	}

	@Focus
	public void setFocus() {
		//tableViewer.getTable().setFocus();
	}

	@Persist
	public void save() {
		dirty.setDirty(false);
	}

	@Override
	public void doubleClick(DoubleClickEvent event) {
		// TODO Auto-generated method stub
		log.logger.Infologg(" [ double click ]" );
		  IStructuredSelection selection = (IStructuredSelection)
			       tableViewer.getSelection();
		  if (selection == null) return ;
		
		BundleContext ctx = InternalPlatform.getDefault().getBundleContext();

		AddEmployeSched man = new AddEmployeSched();

	    IEclipseContext iEclipseContext=null;
		//inject the context into an object
	    //IEclipseContext iEclipseContext was injected into this class
	    ContextInjectionFactory.inject(man,iEclipseContext);

	    man.execute(null,null , null,(Employe)selection.getFirstElement());
		
		
		
		
		
			
	        

	        
	        
	        
	        
	        
	        
		
	}

	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		// TODO Auto-generated method stub
		log.logger.Infologg(" [ Employe select ]" );
		
		  IStructuredSelection selection = (IStructuredSelection)
			       tableViewer.getSelection();
		  if (selection == null) return ;
		BundleContext ctx = InternalPlatform.getDefault().getBundleContext();

		// send message pour double click employe
	    ServiceReference<EventAdmin> ref = ctx.getServiceReference(EventAdmin.class);
        EventAdmin eventAdmin = ctx.getService(ref);
        Map<String,Object> properties = new HashMap<String, Object>();
        properties.put("Employe", (Employe)selection.getFirstElement());
        
        Event event2 = new Event("Employes/select", properties);
        eventAdmin.sendEvent(event2);

	}
	
	
	// MyEventConstants.TOPIC_TODO_UPDATE is 
	// a String constant

	/*@Inject @Optional
	private void getNotified(@UIEventTopic("TEST") 

	//private void getNotified(@Optional@UIEventTopic("TEST") 
	 Map<String,Object>  s) {
	  System.out.println(" MESSAGE RECU "+s);
	} */
	

	

	// Dictionary<String,String> prope = new Hashtable<String, String>();
		
	 
	
		
	    
	     
}